// pages/category/category.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.navBarHeight + app.globalData.statusBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    categoryList: [],
    goodsList: [],
    pageSize: 10,
    pageIndex: 1
  },

  toGoodsDetailPage( event ){
     let goodsId = event.currentTarget.dataset.id;
     wx.navigateTo({
       url: `/pages/goods-detail/goods-detail?id=${goodsId}`,
     })
  },

  changeMenuItem( event ){
    let selectedItem = event.currentTarget.dataset.item;
    let categoryList = this.data.categoryList || [];
    categoryList = categoryList.map(item=>{
      return {
        ...item,
        selected: false
      }
    })
    let findRes = categoryList.find( item=>{
      return item.id == selectedItem.id
    });
    if( findRes ){
      findRes.selected = true;
      this.getGoodsListByCategory(findRes.id);
    }

    this.setData({
       categoryList: [...categoryList]
    })
  },

  getGoodsListByCategory(  categoryId ){
    wx.request({
      url: 'http://localhost:8082/app/omsGoodsInfo/pagination',
      method: "GET",
      data:{
        pageIndex: 1,
        pageSize: 20,
        categoryId
      },
      success: (res) => {
        res = res.data;
        if( res.code != 200 ){
          return;
        }
        let data = res.data;
        let list =  data.list;
        list = list.map( item=>{
          let actualPrice = item.actualPrice+"";
            return {
              ...item,
              actualIntPrice:actualPrice.substring(0, actualPrice.length-2),
              actualFloatPrice: actualPrice.substring(actualPrice.length-2)
            }
          })
        console.log(list)
        this.setData({
          goodsList: list
        })
      },
      fail: (err)=>{
        console.error("2222", err)
      }
    })
  },

  getCategory(){
    wx.request({
      url: 'http://localhost:8082/app/omsGoodsCategory/pagination',
      method: "GET",
      data:{
        pageIndex: 1,
        pageSize: 20
      },
      success: (res) => {
        res = res.data;
        if( res.code != 200 ){
          return;
        }
        let data = res.data;
        let list =  data.list;
        console.log(list)
        list = list.map( (item, index)=>{
          return {
            ...item,
            selected: index == 0
          }
        })
        this.setData({
          categoryList: list
        })

        let defaultCategory = list[0];
        if( defaultCategory ){
            this.getGoodsListByCategory( defaultCategory.id );
        }
      },
      fail: (err)=>{
        console.error("2222", err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCategory();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})